﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SailHero.Soft.FrameWork.DAO.Core;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace SailHero.Soft.FrameWork.DomainRepositories
{
    public class RoleRepository : EFRepository<SYS_ROLE>, IRoleRepository
    {
        public RoleRepository()
            : base("AUTHCODEDBORA")
        {
        }

        /// <summary>
        /// 设置角色
        /// 先删除当前角色下所有的用户
        /// 然后再添加选中的用户
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public string SetUserRole(string userID, string roleID)
        {
            try
            {
                IUserInRoleRepository uinR =new UserInRoleRepository();
                var userInRoleList = uinR.GetList(model => model.ROLEID == roleID).ToList();
                foreach (var mod in userInRoleList)
                {
                    uinR.Delete(mod);  //删除当前角色下所有的用户
                }
                string[] userIDList = userID.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                SYS_USERINROLE urMod;
                foreach (string uID in userIDList)
                {
                    urMod = new SYS_USERINROLE()
                    {
                        ROLEID = roleID,
                        USERID = uID,
                        CREATEDATE = DateTime.Now
                    };
                    uinR.Add(urMod);
                }
                uinR.UnitOfWork.SaveChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 查询某一角色下面所有权限
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public IList GetPermissionListByRole(string roleID)
        {
            IRolePermissionRepository uinR = new RolePermissionRepository();
            return uinR.GetList(model => model.ROLEID == roleID).ToList();
        }


        public string SetPermission(string roleID, string parentID, string subID)
        {
            try
            {
                IRolePermissionRepository uinR = new RolePermissionRepository();
                SYS_ROLEPERMISSION rolePermissionModel;
                var permissionList = uinR.GetList(model => model.ROLEID == roleID).ToList();
                foreach (SYS_ROLEPERMISSION mod in permissionList)
                {
                    uinR.Delete(mod);  //删除该角色下面所有的权限
                }
                string[] parentIDList = parentID.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                ArrayList parentList = new ArrayList();  //父级模块ID列表
                foreach (string pid in parentList)
                {
                    if (parentList.Contains(pid) == false)
                    {
                        parentList.Add(pid);
                        rolePermissionModel = new SYS_ROLEPERMISSION()
                        {
                            ROLEID = roleID,
                            MODELID = pid
                        };
                        uinR.Add(rolePermissionModel);  //添加父级模块
                    }
                }

                string[] subIDList = subID.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string id in subIDList)
                {
                    rolePermissionModel = new SYS_ROLEPERMISSION()
                    {
                        ROLEID = roleID,
                        MODELID = id
                    };
                    uinR.Add(rolePermissionModel);  //添加子级模块
                }
                uinR.UnitOfWork.SaveChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
}